
//to compile: g++ main.cpp tablero.cpp tablero_jugador.cpp tablero_pc.cpp barco.cpp -o play -lglut -lGLU -lGL

#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <time.h>
#include "math.h"
#include <vector>

#include "tablero.h"
#include "tablero_jugador.h"
#include "tablero_pc.h"
#include "barco.h"

using namespace std; 

int w=740, h=480;

//objects

Tablero_Jugador t_jugador; 
//Tablero_PC t_pc;


//functions

void init();
void processNormalKeys(unsigned char key, int xcoor, int ycoor);
void juego();
void changeSize(int w, int h);
void onMouse(int button, int state, int x, int y);

//main

int main(int argc, char ** argv) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(200,50);
	glutInitWindowSize(w,h);
	glutCreateWindow("HUNDIR LA FLOTA");
	init();
		
	glutDisplayFunc(juego);
	glutReshapeFunc(changeSize);
	glutIdleFunc(juego);
	
	glutKeyboardFunc(processNormalKeys);
	glutMouseFunc(onMouse);
	glutMainLoop();

	return 0;
}

void init(){

	glClearColor(0.0,0.0,0.0,1.0);
	glutSwapBuffers();
    
}

void processNormalKeys(unsigned char key, int xcoor, int ycoor) {
	int i=0;

	if (key == 27 )
		exit(0);
	
}

void changeSize(int w, int h) {

	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if (h == 0)
		h = 1;
	float ratio =  w * 1.0 / h;

        // Use the Projection Matrix
	glMatrixMode(GL_PROJECTION);

        // Reset Matrix
	glLoadIdentity();

	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45.0f, ratio, 0.1f, 100.0f);

	// Get Back to the Modelview
	glMatrixMode(GL_MODELVIEW);
}

void onMouse(int button, int state, int x, int y) {
	if ( (button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))   {
		t_jugador.horizontal=true;
		t_jugador.click_casilla(x,y);
}

	if ( (button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))   {
		t_jugador.horizontal=false;
		t_jugador.click_casilla(x,y);
		
}

}

void juego(){

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(	0.0f, 0.0f, 10.0f,
			0.0f, 0.0f,  0.0f,
			0.0f, 1.0f,  0.0f);

	t_jugador.dibujar(-5.0f,-2.0f);
	
	t_jugador.dibujarbarcos();

	glutSwapBuffers();

	glutMouseFunc(onMouse);
	
	glutSwapBuffers();
	
}

